<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Markdown;
use App\Model\Art;
use Auth;
use Captcha;
use Validator;

class ArticleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //$arts = DB::table('arts')->orderBy('id','desc')->paginate(3);

        //$art = new Art();
        //$arts = $art->paginate(3);

        $arts = Art::orderBy('id','desc')->paginate(3);
        return view('art.index',['arts'=>$arts]);

       /* //删除数据
        $res = DB::table('arts')->where('id','>',1)->delete();
        dd($res);

        //修改数据
        $res = DB::table('arts')->where('id',2)->update(['content'=>'update2']);
        dd($res);

        //插入数据
        $art = ['title'=>'2','content'=>'2'];
        $res = DB::table('arts')->insert($art);
        dd($res);

        //查询数据
        $arts = DB::table('arts')->get();
        dd($arts);
        return view('art.index',['title'=>$title]);*/

        //聚合函数
        //$count = DB::table('article')->count();
        /*$max = DB::table('article')->max('cat_id');
        $res = DB::table('article')
            ->groupBy('cat_id')
            ->having('cat_id','>',5)
            ->select('cat_id',DB::raw('count(*)as number'))
            ->get();
        dd($res);*/
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('art.add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //$data = $request->except('_token');
        //$res = DB::table('arts')->insert($data);

        //orm第一种入库方式
        /*$art = new Art();
        $art->title = $request->title;
        $art->content = $request->content;
        $art->save();*/

        //orm第二种入库方式
        //$art = Art::create(['title'=>$request->title,'content'=>$request->content]);

        /* $res = Validator::make($request->all(), [
            'title'     => 'required|max:30|min:8',
            'content'   => 'required|max:1000|min:50',
        ],$messages);*/

        $art = Art::create($request->except('_token'));
        if($art){
            return redirect('/art');
        }
        else{
            return back();
        }

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $art = DB::table('arts')->where('id','=',$id)->first();

        /*$art = Art::find($id);
        $art->content=Markdown::convertToHtml($art->content);*/

        return view('art.show',['art'=>$art]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //$art = DB::table('arts')->where('id','=',$id)->first();

        $art = Art::find($id);
        return view('art.edit',['art'=>$art]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        /*$res = DB::table('arts')->where('id','=',$id)
            ->update(['title'=>$request->title,'content'=>$request->content]);*/

        //update第一种方式
        /*$art = Art::find($id);
        $art->title = $request->title;
        $art->content = $request->content;
        $art->save();*/

        //update第二种方式
        $art = Art::where('id','=',$id)
            ->update($request->except('_token','_method'));
        return redirect('/art');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //$res = DB::table('arts')->where('id',$id)->delete();

        //第一种删除方式
        //$art = Art::find($id)->delete();

        //第二种删除方式
        $art = Art::destroy($id);
        return redirect('/art');
    }

    public function say($num)
    {
        echo 'func say';
        echo"<br>";
        echo "$num";
    }
    public function getAbc()
    {
        echo "abc";
    }

    public function postAb()
    {
        echo "ab";
    }

    public function getMk()
    {
        $str = "## h1";
        echo Markdown::convertToHtml($str);
    }

    public function getUser()
    {
        //打印用户登录信息
        dd(Auth::user());

        //检测用户登录状态
        //dd(Auth::check());
    }

    public function getLogout()
    {
        Auth::logout();
    }

    public function postLogin(Request $request)
    {
        //手动认证用户登录
        $res = Auth::attempt(['email' => $request->email, 'password' => $request->password]);
        if($res){
            return redirect('/art');
        }else{
            return back();
        }
        //dd($res);
    }

    //获取验证码
    public function getCode()
    {
        //返回url
        //return Captcha::src();
        //返回html
        return captcha_img();
    }

    //验证验证码
    public function getVc()
    {
        //获取验证码
        Captcha::check();
    }
}
